package cn.kgc.controller;

import cn.kgc.entity.Bill;
import cn.kgc.entity.User;
import cn.kgc.service.BillService;
import cn.kgc.util.Common;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by Administrator on 2020/3/12.
 */
@Controller
@RequestMapping("sys/bill")
public class BillController {

    @Autowired
    private BillService billService;


    @RequestMapping(value = "toList")
    public String toList(Bill bill, Integer pageNum, Integer pageSize, Model model) {
        if (null == pageNum) {
            pageNum=1;
        }
        if (null == pageSize) {
            pageSize=5;
        }
        PageInfo<Bill> pageInfo = billService.queryByBill(bill, pageNum, pageSize);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("bill",bill);
        return "bill_list";
    }

    @RequestMapping(value="/toSave",method = RequestMethod.GET)
    public String toSave(){
        return "bill_save";
    }

    @ResponseBody
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public String save(Bill bill, HttpSession session){
        User loginUser = (User) session.getAttribute(Common.LOGIN_USER);
        bill.setCreatedBy(loginUser.getId());
        Integer count = billService.save(bill);
        if (count>0){
            return "success";
        }
        return "error";
    }

    @RequestMapping(value="/toDetail",method = RequestMethod.GET)
    public String toDetail(Model model,Integer id){
        Bill bill = billService.queryById(id);
        model.addAttribute("bill",bill);
        return "bill_detail";
    }

    @ResponseBody
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public String delete(Integer id){
        Integer count = billService.delete(id);
        if (count>0){
            return "success";
        }
        return "error";
    }
}
